
package io.zhifei.modules.app.login.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.zhifei.common.utils.DateUtils;
import io.zhifei.modules.app.login.dao.LoginDao;
import io.zhifei.modules.app.login.entity.UserEntity;
import io.zhifei.modules.app.login.form.LoginForm;
import io.zhifei.modules.app.login.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;


@Service("loginService")
public class LoginServiceImpl extends ServiceImpl<LoginDao, UserEntity> implements LoginService {

    @Autowired
    private LoginDao loginDao;

    @Override
    public Map<String, Object> queryByMobile(String tel, String password) {
        //查询是学员还是客户
        Map<String, Object> isMember = loginDao.getMember(tel);
        if (isMember != null) {
            isMember.put("memberOrCustomer", "1");
            return isMember;
        } else {
            Map<String, Object> isCustomer = loginDao.getCustomer(tel);
            isCustomer.put("memberOrCustomer", "0");
            return isCustomer;
        }
    }

    @Override
    public Map<String, Object> login(LoginForm form, HttpServletRequest request) {
        Map<String, Object> user = queryByMobile(form.getTel(), form.getPassWord());

////        mac地址验证
//        String loginDevice = "";
//        String MAC = "";
//        //获取注册设备
//        Map<String, String> map = MACUtils.MAC(request, form);
//        loginDevice = map.get("loginDevice");
//        MAC = map.get("MAC");
//        if (loginDao.MACChec(Long.valueOf(user.get("id").toString()), loginDevice, MAC) != 1) {
//            throw new RRException("请使用注册的设备登录！");
//        }
        //验证是学员登录还是客户登录
        //第一步验证是否是学员
        return user;
    }

    @Override
    public void loginCount(Long userId, Integer memberOrCustomer) {
        //判断是否是学员，是学员就记录登录天数
        if (memberOrCustomer == 1) {
            Map<String, Object> map = loginDao.loginCount(userId);
            String nowDay = DateUtils.format(new Date());
            int day = 1;
            UserEntity userEntity = new UserEntity();
            if (!nowDay.equals(map.get("lastTime")) || StringUtils.isEmpty(map.get("lastTime"))) {
                if (StringUtils.isEmpty(map.get("loginTimes"))) {
                    userEntity.setId(userId);
                    userEntity.setLastLoginTime(nowDay);
                    userEntity.setLoginTimes(day);
                    loginDao.updateById(userEntity);
                } else {
                    userEntity.setId(userId);
                    userEntity.setLastLoginTime(nowDay);
                    Integer loginDays = (Integer) map.get("loginTimes");
                    userEntity.setLoginTimes(loginDays + day);
                    loginDao.updateById(userEntity);
                }
            }
        }
    }

    @Override
    public Map<String, Object> getMemberOrCustomer(Long userId) {
        return loginDao.getMemberOrCustomer(userId);
    }
}
